<template>
	<div class="login_bg">
		<div class="login_adv" style="background-image: url(img/auth_banner.jpg);">
			<div class="login_adv__title">
				<h2>寻衡CLOUD</h2>
				<h4>{{ $t('login.slogan') }}</h4>
				<p>{{ $t('login.describe') }}</p>
			</div>
			<div class="login_adv__bottom">
			 版本：{{$CONFIG.APP_NAME}} {{$CONFIG.APP_VER}}<br><br>电话：158 1736 2414<br><br>邮箱：452794162@qq.com<br><br> © 揭阳寻衡网络科技有限公司
			</div>
		</div>
		<div class="login_main">
			<div class="login-form">
				<div class="login-header">
					<div class="logo">
						<img :alt="$CONFIG.APP_NAME" src="img/logo.png">
<!--						<label>{{$CONFIG.APP_NAME}}</label>-->
					</div>
<!--					<h2>{{ $t('login.signInTitle') }}</h2>-->
				</div>
				<el-form ref="loginForm" :model="ruleForm" :rules="rules" label-width="0" size="large" @keydown.enter="login">
					<el-form-item prop="user">
						<el-input v-model="ruleForm.user" prefix-icon="el-icon-user" clearable :placeholder="$t('login.userPlaceholder')"/>
					</el-form-item>
					<el-form-item prop="password">
						<el-input v-model="ruleForm.password" prefix-icon="el-icon-lock" clearable show-password :placeholder="$t('login.PWPlaceholder')"></el-input>
					</el-form-item>
					<el-form-item prop="imgCode" style="margin-bottom: 10px;">
								<el-input v-model="ruleForm.imgCode"
										  prefix-icon="el-icon-Collection-tag"
										  size="large"
										  clearable
										  placeholder="请输入图片验证码"
										  :maxlength="10"
										  style="width:calc(100% - 105px)"
								/>
								<el-image
									v-loading="loadingCaptcha"
									:src="captchaImg"
									@click="getCaptchaImg"
									alt="加载中"
									style="width:100px;min-height:35px;cursor:pointer;display:block;margin-top: 2px;margin-left: 5px"
								>
									<template #error>
										<div class="image-slot">
											<el-icon><el-icon-picture-filled /></el-icon>
										</div>
									</template>
								</el-image>
					</el-form-item>
					<el-form-item style="margin-bottom: 10px;">
						<el-row style="width: 100%;">
							<el-col :span="12">
								<el-checkbox :label="$t('login.rememberMe')" v-model="ruleForm.saveLogin"></el-checkbox>
							</el-col>
							<el-col :span="12"  class="login-forgot">
<!--								<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>-->
							</el-col>
						</el-row>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn') }}</el-button>
					</el-form-item>
				</el-form>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				isSlideVerifySucess:false,
				loadingCaptcha:true,
				captchaId:"",
				captchaImg: "",
				ruleForm: {
					user: "",
					password: "",
					saveLogin: false,
					imgCode:"",
					device:"PC"
				},
				rules: {
					user: [
						{required: true, message: this.$t('login.userError'), trigger: 'blur'}
					],
					password: [
						{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
					],
					imgCode: [
						{required: true, message: this.$t('login.imgCodeError'), trigger: 'blur'}
					],
				},
				islogin: false,
			}
		},
		created: function() {
			this.$TOOL.cookie.remove("TOKEN")
			this.$TOOL.data.remove("USER_INFO")
			this.$TOOL.data.remove("MENU")
			this.$TOOL.data.remove("PERMISSIONS")
			this.$TOOL.data.remove("MENU")
			this.$TOOL.data.remove("MY_GRID")
			this.$store.commit("clearViewTags")
			this.$store.commit("clearKeepLive")
			this.$store.commit("clearIframeList")
			this.getCaptchaImg();
		},
		methods: {
			onSlideVerifySuccess(r){
				console.log("滑动验证成功！ "+JSON.stringify(r))
				this.isSlideVerifySucess = true;
				return true;
			},
			refreshSlideVerfiy(){
				this.$refs.verify.refresh();
				this.isSlideVerifySucess = false;
			},
			async getCaptchaImg() {
				this.loadingCaptcha = true;
				var res = await this.$API.auth.initCaptcha.get();
				this.loadingCaptcha = false;
				this.captchaId = res.data.captchaId;
				var base64Src = res.data.base64Src;
				this.captchaImg = "data:image/png;base64,"+base64Src;

			},
			async login(){
				var validate = await this.$refs.loginForm.validate().catch(()=>{})
				if(!validate){ return false }
				this.islogin = true
				var data = {
					username: this.ruleForm.user,
					password: this.ruleForm.password,
					code: this.ruleForm.imgCode,
					captchaId: this.captchaId,
					saveLogin: this.saveLogin,
					device:this.ruleForm.device
				}
				//获取token
				var login = await this.$API.auth.login.post(data)
				if(!login.success){
					this.islogin = false
					//this.refreshSlideVerfiy();
					await this.getCaptchaImg();
					return false
				}
				//获取菜单
				var menu = await this.$API.system.menu.myMenus.get()
				if(menu.success){
					if(menu.data.length == 0){
						this.islogin = false
						this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
							type: 'error',
							center: true
						})
						await this.getCaptchaImg();
						//this.refreshSlideVerfiy();
						return false
					}
					this.$TOOL.cookie.set("TOKEN", login.data.token)//set token
					this.$TOOL.data.set("USER_INFO", login.data.user)
					this.$TOOL.data.set("MENU", menu.data.userPermissionList)
					this.$TOOL.data.set("PERMISSIONS", menu.data.userPermissionCodes)
					this.$TOOL.handleCustomConfig(login.data.user.personalConfigs);
				}else{
					this.islogin = false
					await this.getCaptchaImg();
					//this.refreshSlideVerfiy();
					return false
				}

				this.$router.replace({
					path: '/'
				})
				this.$message.success("登录成功")
				this.islogin = false
			},
		}
	}
</script>

<style scoped>
	.login_bg {width: 100%;height: 100%;background: #fff;display: flex;}
	.login_adv {width: 33.33333%;background-color: #555;background-size: cover;background-position: center center;background-repeat: no-repeat;position: relative;}
	.login_adv__title {color: #fff;padding: 40px;position: absolute;top:0px;left:0px;right: 0px;z-index: 2;}
	.login_adv__title h2 {font-size: 40px;}
	.login_adv__title h4 {font-size: 18px;margin-top: 10px;font-weight: normal;}
	.login_adv__title p {font-size: 14px;margin-top:10px;line-height: 1.8;color: rgba(255,255,255,0.6);}
	.login_adv__title div {margin-top: 10px;display: flex;align-items: center;}
	.login_adv__title div span {margin-right: 15px;}
	.login_adv__title div i {font-size: 40px;}
	.login_adv__title div i.add {font-size: 20px;color: rgba(255,255,255,0.6);}
	.login_adv__bottom {position: absolute;left:0px;right: 0px;bottom: 0px;color: #fff;padding: 40px;background-image:linear-gradient(transparent, #000);z-index: 3;}
	.login_adv__mask {position: absolute;top:0px;left:0px;right: 0px;bottom: 0px;background: rgba(0,0,0,0.5);z-index: 1;}

	.login_main {flex: 1;overflow: auto;display:flex;}
	.login-form {width: 400px;margin: auto;padding:20px 0;}
	.login-header {margin-bottom: 40px;}
	.login-header .logo {align-items: center;text-align: center;}
	.login-header .logo img {width: 314px;height: 85px;vertical-align: bottom;}
	.login-header .logo label {font-size: 24px;}
	.login-header h2 {font-size: 24px;font-weight: bold;margin-top: 20px;}
	.login-oauth {display: flex;justify-content:space-around;}
	.login-form .el-divider {margin-top:40px;}

	.login-form:deep(.login-forgot) {text-align: right;}
	.login-form:deep(.login-forgot) a {color: var(--el-color-primary);}
	.login-form:deep(.login-forgot) a:hover {color: var(--el-color-primary-light-3);}

	.login_config {position: absolute;top:20px;left: 20px;}
	.el-dropdown-menu__item.selected {color: var(--el-color-primary);}

	.image-slot {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100%;
		color: var(--el-text-color-secondary);
		font-size: 30px;
	}

	.image-slot .el-icon {
		font-size: 30px;
	}

	@media (max-width: 1200px){
		.login-form {width: 340px;}
	}
	@media (max-width: 992px){
		.login_main {display: block;}
		.login-form {width:100%;padding:20px 40px;}
		.login_adv {display: none;}
	}
</style>
